<template>
  <div>
    <!-- 添加或修改产品分类对话框 -->
    <el-dialog
      :model-value="open"
      :close-on-click-modal="false"
      :title="title"
      append-to-body
    >
      <el-form
        ref="editForm"
        :model="form"
        :rules="rules"
        label-width="110px"
      >
        <el-form-item label="分类名称" prop="classifyName">
          <el-input v-model="form.classifyName" style="width: 280px;" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类代码" prop="classifyCode">
          <el-input
            v-model="form.classifyCode"
            type="Number"
            placeholder="请输入分类代码"
            style="width: 280px;"
          />
        </el-form-item>
        <el-form-item label="产品分组名称" prop="productsGroupIdList">
          <el-select
            v-model="form.productsGroupIdList"
            filterable
            placeholder="请选择"
            clearable  
            multiple
          >
            <el-option
              v-for="dict in dicts[DictDirection.产品分组]"
              :key="dict.id"
              :label="dict.groupName"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dicts[DictDirection.通用状态]" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, defineExpose, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import useDicts, { DictDirection } from '@/store/modules/dicts'
import { getClassify, addClassify, updateClassify } from '@/api/basicConfig/productsClassify'

const dicts = useDicts()
const props = defineProps({
  open: {
    type: Boolean,
  },
  title: {
    type: String,
  },
})
dicts.setDicts(DictDirection.通用状态)
dicts.setDicts(DictDirection.产品分组)
// 表单参数
let form = ref({
    id: undefined,
    classifyCode: undefined,
    classifyName: undefined,
    productsGroupIdList: undefined,
    status: undefined,
    remark: undefined,
  }),
  // 表单校验
  rules = {
    classifyName: [{ required: true, message: '产品分类名称不能为空', trigger: ['blur'] }],
    classifyCode: [{ required: true, message: '产品分代码不能为空', trigger: ['blur'] }],
    productsGroupIdList: [{ required: true, message: '产品分类名称不能为空', trigger: ['change', 'blur'] }],
  },
  // 新增修改表单
  editForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()

const emit = defineEmits(['update:open', 'changeList'])

// 重置新增修改列表
const reset = () => {
  form.value = {
    id: undefined,
    classifyCode: undefined,
    classifyName: undefined,
    productsGroupIdList: undefined,
    status: undefined,
    remark: undefined,
  }
  $resetForm(editForm.value)
}
// 新增修改——确定
const submitForm = () => {
  $validateForm(editForm.value, () => {
    // form.value.productsGroupIdList = form.value.productsGroupIdList.join(',')
    if (form.value.id == undefined) {
      addClassify(form.value).then((res) => {
        if (res.code == 200) {
          ElMessage.success('新增成功')
          emit('update:open', false)
          reset()
          emit('changeList')
        }
      })
    } else {
      updateClassify(form.value).then((res) => {
        if (res.code == 200) {
          ElMessage.success('修改成功')
          // console.log(form.value)
          reset()
          emit('update:open', false)
          emit('changeList')
        }
      })
    }
  })
}
// 查询修改
const getList = (id) => {
  getClassify(id).then((res) => {
    const productsGroupId = res.data.productsGroupId
    const data = !!productsGroupId && productsGroupId.split(',').map(el => Number(el))
    res.data.productsGroupIdList = data
    form.value = res.data
    console.log(form.value)
  })
}
// 修改新增——取消
const cancel = () => {
  emit('update:open', false)
  reset()
}

defineExpose({
  form,
  getList,
})
</script>
